package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.Counts;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SmallVideosService;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Administrator
 */
@RestController
@RequestMapping(path = "/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SmallVideosService videosService;

    /**
     * 查询用户基础信息
     *
     * @param userID
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID, @RequestHeader("Authorization") String token) {
        //1. 校验token
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if(!verifyToken) {
//            return ResponseEntity.status(401).body(null);
//        }
//        //2. 获取token中的用户信息
//        Claims claims = JwtUtils.getClaims(token);
//        String id = claims.get("id").toString();
        Long userId = UserHolder.getUserId();
        if (userID == null) {
            userID = userId;
        }

        UserInfoVo userInfo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     * @param userInfo
     * @param token
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
//        //1、判断token是否合法
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if(!verifyToken) {
//            return ResponseEntity.status(401).body(null);
//        }
//        //2、获取id
//        Claims claims = JwtUtils.getClaims(token);
//        Integer id = (Integer) claims.get("id");
        Long userId = UserHolder.getUserId();
        //设置id
        userInfo.setId(Long.valueOf(userId));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Counts counts = userInfoService.counts();
        return ResponseEntity.ok(counts);
    }

    /**
     * 客户端更新用户头像
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) throws IOException {
        userInfoService.updateHead(headPhoto,UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * @param page
     * @param pagesize
     * @param type 1.互相关注  2.我关注  3.粉丝  4.谁看过我
     * @param nickname
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@RequestParam(defaultValue = "1")  Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  @PathVariable("type") String type,
                                  String nickname){
        PageResult pageResult = userInfoService.friends(page,pagesize,type,nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 粉丝喜欢
     * @param uid
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable("uid") String uid){
        userInfoService.saveUserLike(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 喜欢取消
     * @param uid
     * @return
     */
    @DeleteMapping("/fans/{uid}")
    public ResponseEntity deleteFans(@PathVariable("uid") String uid){
        userInfoService.removeUserLike(uid);
        return ResponseEntity.ok(null);
    }
}